'use strict';
const puppeteer = require('puppeteer-core');
const fs = require('fs');

const http = require('http');

(async () => {
  const startUrl = 'https://detail.tmall.com/item.htm?id=681120604165&pvid=b97eca08-9918-44ea-9efb-015d5963a1b7&scm=1007.40986.275655.0&spm=a21bo.jianhua.201876.2.5af911d9VmPkdo';
  const browser = await puppeteer.launch({
    executablePath: `C:/Program Files/Google/Chrome/Application/chrome.exe`,
    headless: false, // 是否界面
    defaultViewport: { // 界面设置
      width: 1820,
      height: 1080,
    },
    ignoreHTTPSErrors: false, // 忽略 https 报错
    args: ['--start-maximized', '--no-sandbox', '--disable-setuid-sandbox'],
    ignoreDefaultArgs: ['--enable-automation','--disable-extensions'],
  });
  const page = await browser.newPage();
  await page.goto(startUrl).catch(error => console.log(error));

  // await browser.close();
})();

const startRequest = async (links, page) => {
  let i = 0;
  while (i < links.length) {
    await doit(links, i)
    i++;
  }
}

const doit = async (links, i) => {
  //采用http模块向服务器发起一次get请求
  http.get(links[i].href, async (res) => {
    let html = ''; //用来存储请求网页的整个html内容
    res.setEncoding('utf-8'); //防止中文乱码
    //监听data事件，每次取一块数据
    res.on('data', async (chunk) => {
      html += chunk;
    });
    //监听end事件，如果整个网页内容的html都获取完毕，就执行回调函数
    res.on('end', async () => {
      console.log(links[i].title);
      await savedContent(html, links[i].title, i);
    });
  })
}

//在本地存储所爬取的内容
const savedContent = async (html, name, i) => {
  fs.appendFile(`PDF/${i + 1}.${name}.html`, html, 'utf-8', function (err) {
    if (err) {
      console.log(err);
    } else {
      console.log(name + "成功。");
    }
  });
}